﻿using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;

public class UIMessageBox : Singleton<UIMessageBox>
{
    private Button closeButton;
    [SerializeField]
    private Button m_buttonPrefab;
    [SerializeField]
    private Text m_messageText;
    [SerializeField]
    private Text m_titleText;
    private Action okAction;
    private Button okButton;

    private void Awake()
    {
        this.m_buttonPrefab.gameObject.SetActive(false);
        this.okButton = this.m_buttonPrefab.CloneGameObjectAsChild<Button>(this.m_buttonPrefab.transform.parent, true);
        this.okButton.onClick.AddListener(() => this.okAction());
        this.okButton.GetComponentInChildren<Text>().text = "OK";
        this.closeButton = this.m_buttonPrefab.CloneGameObjectAsChild<Button>(this.m_buttonPrefab.transform.parent, true);
        this.closeButton.gameObject.SetActive(true);
        this.closeButton.onClick.AddListener(() => base.gameObject.SetActive(false));
        this.closeButton.GetComponentInChildren<Text>().text = "Close";
    }

    public void Show(string title, string message, Action okAction = null)
    {
        base.gameObject.SetActive(true);
        this.okAction = okAction;
        this.okButton.gameObject.SetActive(okAction != null);
        this.m_titleText.text = title;
        this.m_messageText.text = message;
    }
}

